<template>
  <basic-dialog
    title="编辑证书"
    :visible="visible"
    :loading="loading"
    :width="640"
    has-footer
    @open="handleOpen"
    @closed="emit('update:visible', false)"
    @submit="handleSubmit"
  >
    <el-form
      ref="formRef"
      :model="formState"
      :rules="rules"
      require-asterisk-position="right"
      label-width="100px"
      @submit.prevent
    >
      <el-form-item label="域名" prop="primary_domain">
        <el-input
          v-model="formState.primary_domain"
          placeholder="请输入域名，支持*和IP地址"
        />
      </el-form-item>

      <el-form-item label="备注" class="hide-star" prop="description">
        <el-input
          v-model.trim="formState.description"
          maxlength="20"
          placeholder="请输入备注"
        />
      </el-form-item>

      <el-form-item prop="key_type" label="密钥算法">
        <el-select v-model="formState.key_type" class="w-full">
          <el-option
            v-for="item in ALGORITHM_OPTS"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="auto_renew" label="">
        <el-checkbox v-model="formState.auto_renew">自动续签</el-checkbox>
      </el-form-item>
    </el-form>
  </basic-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'

import type { CertUpdateModel } from '@/apis/model/cert'
import { ALGORITHM_OPTS } from '@/config/constant/cert'
interface IProps {
  visible: boolean
  record: any
  loading: boolean
}
interface IEmits {
  (event: 'update:visible', value: boolean): void
  (event: 'update:loading', value: boolean): void
  (event: 'refresh'): void
}

const props = defineProps<IProps>()
const emit = defineEmits<IEmits>()
const formRef = ref<FormInstance>()

const formState = reactive<CertUpdateModel>({
  primary_domain: '',
  description: '',
  key_type: 'EC256',
  valid_method: 1,
  auto_renew: true
})
const validateDomain = (_rule: any, value: any, callback: any) => {
  if (!value) return callback()
  const num = value.length
  let isOut
  value = value.trim()
  if (num > 100) {
    return new Error('域名总长度不能超过100个字符')
  }
  if (value.startsWith('*.')) {
    value = value.substring(2, value.length - 1)
  }
  if (wildcardDomainReg.test(value) || ipv4Reg.test(value)) {
    isOut = false
  } else {
    isOut = true
  }
  if (isOut) {
    callback(new Error('域名格式不正确'))
  } else {
    callback()
  }
}
const rules: Record<string, FormItemRule[]> = {
  primary_domain: [
    { required: true, message: '请输入域名', trigger: 'blur' },
    {
      validator: validateDomain
    }
  ],
  key_type: [{ required: true, message: '请选择', trigger: 'change' }]
}
const handleOpen = () => {
  const record = props.record
  if (record) Object.assign(formState, record)
}
async function handleSubmit() {
  await formRef.value?.validate()
  const params = {
    ...formState,
    id: props.record?.id
  }
  updateCertApi(params)
    .then(() => {
      emit('refresh')
      emit('update:visible', false)
      ToastUtils.success('操作成功')
    })
    .finally(() => emit('update:loading', false))
}
</script>

<style lang="scss" scoped></style>
